var util = require('util');

app.get('/port/add/:device', function(req, res) {
	res.local('title', 'Add Port');
	model.Device.findById(req.params.device, function(e, device) {
		if (e)
			log.error(e, "Controls/Ports:7");
		if (device) {
			model.Port.find({}, [ '_id', 'sysId' ], function(e, inputs) {
				if (inputs) {
					log.debug("Add Port... Device " + device._id);
					res.local('port', {
						device : device._id
					});
					res.local('inputs', inputs);
					res.local('action', 'add');
					res.render('Port/edit.jade', {
						pretty : true
					});
				} else {
					res.redirect('https://' + req.header('X-Request-Host')
							+ '/port');
				}
			});
		} else {
			res.redirect('https://' + req.header('X-Request-Host') + '/port');
		}
	});
});

app.get('/port/:id/edit', function(req, res) {
	res.local('title', 'Edit Port');
	res.local('scripts', {
		'selectPlugin' : '/static/script/pluggin/select.js'
	});
	model.Port.findById(req.params.id, function(e) {
		if (e)
			log.error(e, 'Controls/Ports:29');
	}).populate('device').populate('inputs').run(function(e, port) {
		if (e)
			log.error(e, 'Controls/Ports:33');
		if (port) {
			model.Port.find({}, [ '_id', 'sysId' ], function(e, inputs) {
				if (e)
					log.error(e, 'Controls/Ports:37');
				log.debug('Found Port. Edit');
				res.local('port', port);
				res.local('inputs', inputs || []);
				res.local('action', 'update');
				res.render('Port/edit.jade', {
					pretty : true
				});
			});
		} else {
			res.redirect('https://' + req.header('X-Request-Host') + '/port');
		}
	});
});

app.get('/port/:id/remove', function(req, res) {
	res.local('title', 'Remove Port');
	model.Port.findById(req.params.id, function(e, port) {
		port.remove(function(e) { // must be called on Mongoose Object to
			// trigger event
			if (e)
				log.error(e, 'Controls/Ports:55');
			res.redirect('https://' + req.header('X-Request-Host')
					+ req.param('next', '/port'));
		});
	});
});

app.get('/port/:id', function(req, res) {
	res.local('title', 'Port');
	res.local('scripts', {
		'updatePlugin' : '/static/script/pluggin/update.js'
	});
	model.Port.findById(req.params.id, function(e) {
		if (e)
			log.error(e, 'Controls/Ports:78');
	}).populate('device').populate('inputs').sort('inputs.sysId', -1).run(function(e, port) {
		if (e)
			log.error(e, 'Controls/Ports:72');
		if (port) {
			log.debug('Found Port');
			res.local('port', port);
			res.render('Port/view.jade', {
				pretty : true
			});
		} else {
			res.redirect('https://' + req.header('X-Request-Host') + '/port');
		}
	});
});

app.get('/port', function(req, res) {
	res.local('title', 'Ports');
	res.local('scripts', {
		'updatePlugin' : '/static/script/pluggin/update.js'
	});
	model.Port.find(function(e) {
		if (e)
			log.error(e, 'Controls/Ports:89');
	}).populate('device', [ 'name' ]).sort('sysId', 1).run(function(e, ports) {
		res.local('ports', ports);
		res.render('Port/list.jade');
	});
});

app.post('/port', function(req, res) {
	log.debug('Change Port');

	if (typeof (req.body.action) != 'undefined') {
		log.debug('Action: ' + req.body.action);
		switch (req.body.action) {
		case 'add':
			var p = new model.Port({
				name : req.body.name,
				device : req.body.device,
				sysId : req.body.id,
				type : req.body.type,
				direction : req.body.direction,
				description : req.body.description
			});
			p.save(function(e) {
				if (e)
					log.error(e, 'Controls/Ports:117');
				model.Device.findOne({
					_id : p.device
				}, function(e, device) {
					log.debug("Port " + p._id);

					log.debug("Add to Device " + device._id);
					device.ports.push(p._id);
					log.debug("Pushed");
					device.save(function(e) {
						if (e)
							log.error(e, "Controls/Ports:128");
						p.save(function(e) {
							if(e)
								log.error(e, "Controls/Ports:131");
						});
					});
				});
				res.redirect('https://' + req.header('X-Request-Host')
						+ '/port/' + p._id);
			});
			break;
		case 'update':
			util.puts(util.inspect(req.body));
			model.Port.update({
				_id : req.body._id
			}, {
				name : req.body.name,
				sysId : req.body.id,
				type : req.body.type,
				direction : req.body.direction,
				smoothing : req.body.smooth,
				inputs : req.body.inputs,
				relation : req.body.relation,
				description : req.body.description
			}, function(e) {
				if (e)
					log.error(e, 'Controls/Ports:158');
				res.redirect('https://' + req.header('X-Request-Host')
						+ '/port/' + req.body._id);
			});
			break;
		default:
			res.redirect('https://' + req.header('X-Request-Host') + '/port/'
					+ req.body._id);
		}
	}
});
